package com.net.xpay.common.validator;

import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.UserAccount;
import com.net.xpay.core.manager.UserAccountManager;
import com.net.xpay.core.model.UserWithdrawModel;
import com.net.common.exception.BusinessException;
import com.net.common.util.SerializationUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UserWithdrawVaildator {

    @Autowired
    private CheckCodeManager checkCodeManager;

    @Autowired
    private CheckCodeValidator checkCodeValidator;

    @Autowired
    private UserAccountManager userAccountManager;

    public void validateWithdrawInfo(User user, UserWithdrawModel userWithdrawModel) {
        log.info("validateWithdrawInfo params = {},userId = {}", SerializationUtil.obj2String(userWithdrawModel), user.getId());
        //验证姓名
        if (StringUtils.isEmpty(userWithdrawModel.getRealName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "姓名不能为空");
        }

        if (StringUtils.isEmpty(userWithdrawModel.getAlipayNickName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号不能为空");
        }

        if (StringUtils.isEmpty(userWithdrawModel.getCheckCode())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "验证码不正确");
        }

        //验证手机验证码
        CheckCode checkCode = checkCodeManager.findByMobileAndType(user.getMobile(), CheckCodeType.WITHDRAW);
        checkCodeValidator.validateCheckCode(checkCode, userWithdrawModel.getCheckCode());


        //验证余额
        UserAccount userAccount = userAccountManager.getById(user.getId());
        if (userAccount.getMoneyLeft() < 100L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "提现金额至少为1元");
        }
    }

}
